package com.eedi.framework.notify.service;

import com.eedi.framework.common.enums.UserTypeEnum;
import com.eedi.framework.common.enums.YesOrNoEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.notify.controller.sys.vo.message.SysNotifyMessageMyPageReq;
import com.eedi.framework.notify.controller.sys.vo.message.SysNotifyMessagePageReq;
import com.eedi.framework.notify.dal.dataobject.SysNotifyMessageDO;
import com.eedi.framework.notify.dal.dataobject.SysNotifyTemplateDO;
import com.eedi.framework.notify.dal.mapper.NotifyMessageMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 站内信 Service 实现类
 *
 * @author xrcoder
 */
@Service
@Validated
public class SysNotifyMessageServiceImpl implements SysNotifyMessageService {

    @Resource
    private NotifyMessageMapper notifyMessageMapper;

    @Override
    public String createNotifyMessage(String userId, UserTypeEnum userType,
                                      SysNotifyTemplateDO template, String templateContent, Map<String, Object> templateParams) {
        SysNotifyMessageDO message = new SysNotifyMessageDO();
        message.setSysUserId(userId);
        message.setSysUserType(userType);
        message.setSysNotifyMessageId(template.getSysNotifyTemplateId());
        message.setSysNoticeCode(template.getSysNotifyCode());
        message.setSysNoticeType(template.getSysNotifyType());
        message.setSysNoticeNickname(template.getSysNotifyNickname());
        message.setSysNoticeContent(templateContent);
        message.setSysNoticeParams(templateParams);
        message.setSysNoticeReadStatus(YesOrNoEnum.NO);
        notifyMessageMapper.insert(message);
        return message.getSysNotifyMessageId();
    }

    @Override
    public PageResult<SysNotifyMessageDO> getNotifyMessagePage(SysNotifyMessagePageReq pageReqVO) {
        return notifyMessageMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<SysNotifyMessageDO> getMyMyNotifyMessagePage(SysNotifyMessageMyPageReq pageReqVO, String userId, UserTypeEnum userType) {
        return notifyMessageMapper.selectPage(pageReqVO, userId, userType);
    }

    @Override
    public SysNotifyMessageDO getNotifyMessage(Long id) {
        return notifyMessageMapper.selectById(id);
    }

    @Override
    public List<SysNotifyMessageDO> getUnreadNotifyMessageList(String userId, UserTypeEnum userType, Integer size) {
        return notifyMessageMapper.selectUnreadListByUserIdAndUserType(userId, userType, size);
    }

    @Override
    public Long getUnreadNotifyMessageCount(String userId, UserTypeEnum userType) {
        return notifyMessageMapper.selectUnreadCountByUserIdAndUserType(userId, userType);
    }

    @Override
    public int updateNotifyMessageRead(Collection<Long> ids, String userId, UserTypeEnum userType) {
        return notifyMessageMapper.updateListRead(ids, userId, userType);
    }

    @Override
    public int updateAllNotifyMessageRead(String userId, UserTypeEnum userType) {
        return notifyMessageMapper.updateListRead(userId, userType);
    }

}
